package mr.go.set.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import mr.go.set.AggregatedSet;
import mr.go.set.samples.Counter;

import org.junit.Before;
import org.junit.Test;

public class SetContractTestCase {

	private AggregatedSet<String, Integer>	set;

	private final String[]					STRINGS	= new String[]
													{ "Two roads diverged in a yellow wood", "And sorry I could not travel both", "And be one traveler, long I stood", "And looked down one as far as I could", "To where it bent in the undergrowth;", "Then took the other, as just as fair", "And having perhaps the better claim", "Because it was grassy and wanted wear", "Though as for that the passing there", "Had worn them really about the same",

													"And both that morning equally lay", "In leaves no step had trodden black.", "Oh, I kept the first for another day!", "Yet knowing how way leads on to way", "I doubted if I should ever come back.",

													"I shall be telling this with a sigh", "Somewhere ages and ages hence:", "Two roads diverged in a wood, and I--", "I took the one less traveled by", "And that has made all the difference." };

	@Before
	public void setUp() throws Exception {
		set = new AggregatedSet<String, Integer>(new Counter());
		List<String> poem = Arrays.asList(STRINGS);
		set.addAll(poem);
	}

	@Test
	public final void testAddExisting() {
		boolean result = set.add("Two roads diverged in a yellow wood");
		assertFalse(result);
	}

	@Test
	public final void testAddNew() {
		String testString = "AAAAAAAAAAAAAA";
		boolean result = set.add(testString);
		assertTrue(result);
		Integer count = set.getCurrentValue(testString);
		assertNotNull(count);
		assertEquals(1, count.intValue());
	}

	@Test
	public final void testClear() {
		set.clear();
		assertEquals(0, set.size());
	}

	@Test
	public final void testContainsAll() {
		Collection<String> testCollection1 = Arrays.asList(
				"I took the one less traveled by",
				"And that has made all the difference.");
		Collection<String> testCollection2 = Arrays.asList("A", "B", "C");
		boolean result = set.containsAll(testCollection1);
		assertTrue(result);
		result = set.containsAll(testCollection2);
		assertFalse(result);
	}

	@Test
	public final void testContainsObject() {
		String testString1 = "I took the one less traveled by";
		String testString2 = "A";
		boolean result = set.contains(testString1);
		assertTrue(result);
		assertEquals(1, set.getCurrentValue(testString1).intValue());
		result = set.contains(testString2);
		assertFalse(result);
	}

	@Test
	public final void testRemoveAll() {
		String testString = "A";
		List<String> poem = Arrays.asList(STRINGS);
		set.add(testString);
		set.removeAll(poem);
		assertEquals(1, set.size());
		assertTrue(set.contains(testString));
	}

	@Test
	public final void testRemoveObject() {
		String testString1 = "A";
		String testString2 = "Two roads diverged in a yellow wood";
		boolean result = set.remove(testString1);
		assertFalse(result);
		result = set.remove(testString2);
		assertTrue(result);
	}

	@Test
	public final void testRetainAll() {
		String testString = "A";
		List<String> poem = Arrays.asList(STRINGS);
		set.add(testString);
		boolean result = set.retainAll(poem);
		assertTrue(result);
		assertFalse(set.contains(testString));
	}

}
